package com.hoshiibuy.company.service.impl;

import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.company.entity.ComDepartments;
import com.hoshiibuy.company.feign.IdGeneratorService;
import com.hoshiibuy.company.mapper.IAttachmentTypeMapper;
import com.hoshiibuy.company.entity.AttachmentType;
import com.hoshiibuy.company.service.IAttachmentTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 附件类型 服务实现类
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Service
public class AttachmentTypeServiceImpl extends ServiceImpl<IAttachmentTypeMapper, AttachmentType> implements IAttachmentTypeService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * @param id
     * @return AttachmentType
     * @description: 根据id查询附件类型信息
     */
    @Cacheable(cacheNames = "attachmenttype", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public AttachmentType getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param
     * @return boolean
     * @description: 新增附件类型
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "attachmenttype", allEntries = true)
    @Override
    public boolean save(AttachmentType entity) {

        String id = idGeneratorService.queryGeneratorId();

        if (id == null) {
            throw new ServiceException(CommonError.error("新增附件类型失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        entity.setModifiedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增附件类型失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改附件类型信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "attachmenttype", allEntries = true)
    @Override
    public boolean updateById(AttachmentType entity) {

        entity.setModifiedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改附件类型失败"));
        }
        return true;
    }
}
